var cloudURL = 'http://1.dtvrecommender.appspot.com/recommender';
//var cloudURL = 'http://localhost:8888/recommender';

var profileForClient = null;

$(document).ready(function() {
	showMessages();
});

function showMessages(message) {
	$('#messages').html(message);
	$('#messages').show();
	window.setTimeout(function() {
		$('#messages').hide();
	}, 5000);
}

function sendClientProfile() {
	$.ajax({
		type : 'POST',
		dataType : 'xml',
		contentType : 'text/html',
		url : cloudURL,
		data : $('#clientProfile').val(),
		success : function(response) {
			showMessages(response);
		}
	});
}

function createChannel() {
	var name = $('#channelName').val();
	var serverLocation = $('#channelServerLocation').val();

	if (name == '' || serverLocation == '') {
		alert('Channel name or server location can\'t be empty.');
		return;
	}

	var xmlText = '<channel action="create" name="' + name + '" resource="'
			+ serverLocation + '"/>';

	$.ajax({
		type : 'POST',
		url : cloudURL,
		data : xmlText,
		success : function(response) {
			showMessages(response);
			getChannelList();
			$('#channelName').val('');
			$('#channelServerLocation').val('');
		}
	});
}

function getChannelList() {
	$('#channelListTable').show();
	var xmlText = '<channel action="list"/>';

	$('#channels').html('');

	$
			.ajax({
				type : 'POST',
				url : cloudURL,
				data : xmlText,
				success : function(response) {
					$(response)
							.find('channel')
							.each(
									function() {
										var name = $(this).attr('name');
										var server = $(this).attr('resource');
										var html = '<tr class="channel">'
												+ '<td>'
												+ name
												+ '</td><td>'
												+ '<a href="'
												+ server
												+ '" target="_blank">'
												+ server
												+ '</a></td><td><a href="#" onclick="javascript: viewPrograms(\''
												+ name
												+ '\');">View Programs</a>&nbsp;|&nbsp;'
												+ '<a href="#" onclick="javascript: deleteChannel(\''
												+ name
												+ '\');">Delete</a></td></tr>';

										$('#channels').append(html);
									});
				}
			});
}

function deleteChannel(channelName) {
	var xmlText = '<channel action="delete" name="' + channelName + '"/>';

	$.ajax({
		type : 'POST',
		url : cloudURL,
		data : xmlText,
		success : function(response) {
			showMessages(response);
			getChannelList();
		}
	});
}

function viewPrograms(channelName) {
	$('#programs').html('');
	$('#programListOfChannel').show();
	$('#labelProgramListOfChannel').html(
			'Program List of ' + channelName + ' Channel');

	var xmlText = '<program action="list-by-channel" channel="' + channelName
			+ '"/>';

	$.ajax({
		type : 'POST',
		url : cloudURL,
		data : xmlText,
		success : function(response) {
			$(response).find('program').each(
					function() {
						var title = $(this).attr('title');
						var genre = $(this).attr('genre');
						var html = '<tr class="program"><td>' + title
								+ '</td><td>' + genre + '</td>';

						$('#programs').append(html);
					});
			if ($('#programs').html() == '') {
				$('#programs').html(
						'<tr><td colspan="2">'
								+ 'No programs association for this channel.'
								+ '</td></tr>');
			}
		}
	});
}

function createClient() {
	var hash = $('#clientId').val();

	if (hash == '') {
		alert('Client ID can\'t be empty.');
		return;
	}

	var xmlText = '<client action="create" hash="' + hash + '"/>';

	$.ajax({
		type : 'POST',
		url : cloudURL,
		data : xmlText,
		success : function(response) {
			showMessages(response);
			getClientList();
			$('#clientId').val('');
		}
	});
}

function getClientList() {
	$('#clientListTable').show();
	var xmlText = '<client action="list"/>';

	$('#clients').html('');

	$
			.ajax({
				type : 'POST',
				url : cloudURL,
				data : xmlText,
				success : function(response) {
					$(response)
							.find('client')
							.each(
									function() {
										var hash = $(this).attr('hash');
										var html = '<tr class="client"><td>'
												+ hash
												+ '</td><td><a href="#" id="'
												+ hash
												+ '" onclick="javascript: $(\'#profileForm\').show(); profileForClient = $(this).attr(\'id\'); getProgramList();">Create Profile</a>&nbsp;|&nbsp;<a href="#" onclick="javascript: deleteClient(\''
												+ hash
												+ '\');">Delete</a></td></tr>';

										$('#clients').append(html);
									});
				}
			});
}

function deleteClient(hash) {
	var xmlText = '<client action="delete" hash="' + hash + '"/>';

	$.ajax({
		type : 'POST',
		url : cloudURL,
		data : xmlText,
		success : function(response) {
			showMessages(response);
			getClientList();
		}
	});
}

function getProgramList() {
	$('#programListTable').show();
	var xmlText = '<program action="list"/>';

	$.ajax({
		type : 'POST',
		url : cloudURL,
		data : xmlText,
		success : function(response) {
			$(response).find('program').each(
					function() {
						var title = $(this).attr('title');
						var channel = $(this).attr('channel');
						var option = '<option value="' + title + '">' + title
								+ ' : ' + channel + '</option>';

						$('#profilePrograms').append(option);
					});
			if ($('#programs').html() == '') {
				$('#programs').html(
						'<tr><td colspan="2">'
								+ 'No programs association for this channel.'
								+ '</td></tr>');
			}
		}
	});
}

function createProfile() {
	var rating = $('#profileRating').val();

	if (rating == '') {
		alert('Rating can\'t be empty.');
		return;
	}

	var xmlText = '<profile action="create" program="'
			+ $('#profilePrograms').val() + '" client="' + profileForClient
			+ '" rating="' + rating + '"/>';

	$.ajax({
		type : 'POST',
		url : cloudURL,
		data : xmlText,
		success : function(response) {
			showMessages(response);
			getProfileList();
			$('#profileRating').val('');
		}
	});
}

function getProfileList() {
	var xmlText = '<profile action="list"/>';

	$('#profiles').html('');

	$
			.ajax({
				type : 'POST',
				url : cloudURL,
				data : xmlText,
				success : function(response) {
					$(response)
							.find('profile')
							.each(
									function() {
										var client = $(this).attr('client');
										var program = $(this).attr('program');
										var rating = $(this).attr('rating');
										var html = '<tr class="profile"><td>'
												+ client
												+ '</td><td>'
												+ program
												+ '</td><td>'
												+ rating
												+ '</td><td><a href="#" onclick="javascript: deleteProfile(\'\');">Delete</a></td></tr>';

										$('#profiles').append(html);
										$('#profileListTable').show();
									});
				}
			});
}

function stage(n) {
	for ( var i = 1; i <= n; i++) {
		$.ajax({
			type : "GET",
			url : "http://localhost:8888/profiles/Profile-" + i + ".xml",
			dataType : "html",
			async : false,
			success : function(data) {
				$("#clientProfile").val(data);
				sendClientProfile();
			}
		});
	}
}
